package com.runa.persistence;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 2024/9/14 12:54
 * zhangYB
 */
@Slf4j
public class OutputUtil {



    public static void output(List<String> list) {
        FileWriter fileWriter = null;
        try {
//            fileWriter = new FileWriter(System.currentTimeMillis() + ".log");
            fileWriter = new FileWriter("logs/output.log");

            for (String s : list) {
                fileWriter.write(s + "\n");
            }

        } catch (IOException e) {
            log.error("", e);
        } finally {
            if (null != fileWriter) {
                try {
                    fileWriter.close();
                } catch (Exception e) {
                    log.error("", e);
                }
            }

        }
    }


    public static List<String> getOutputList(Map<String, Map<String, Map<String, List<Object>>>> taskMap) {
        List<String> list = new ArrayList<>();
        Iterator<String> iterator = taskMap.keySet().iterator();
        while (iterator.hasNext()) {
            String taskId = iterator.next();
            Map<String, Map<String, List<Object>>> netSerialMap = taskMap.get(taskId);
            for (String netSerial : netSerialMap.keySet()) {
                Map<String, List<Object>> nodeMap = netSerialMap.get(netSerial);
                for (String nodeId : nodeMap.keySet()) {
                    List<Object> nodeList = nodeMap.get(nodeId);
                    for (Object o : nodeList) {
                        list.add(taskId + ": " + netSerial + ": " + nodeId + ": " + JSON.toJSONString(o));
                    }
                }
            }
        }
        return list;
    }


    public static void output(Map<String, Map<String, Map<String, List<Object>>>> taskMap) {
        FileWriter fileWriter = null;
        try {
//            fileWriter = new FileWriter(System.currentTimeMillis() + ".log");
            fileWriter = new FileWriter("logs/output.log");

            Iterator<String> iterator = taskMap.keySet().iterator();
            while (iterator.hasNext()) {
                String taskId = iterator.next();
                Map<String, Map<String, List<Object>>> netSerialMap = taskMap.get(taskId);
                for (String netSerial : netSerialMap.keySet()) {
                    Map<String, List<Object>> nodeMap = netSerialMap.get(netSerial);
                    for (String nodeId : nodeMap.keySet()) {
                        List<Object> nodeList = nodeMap.get(nodeId);
                        for (Object o : nodeList) {
                            fileWriter.write(taskId + ": " + netSerial + ": " + nodeId + ": " + JSON.toJSONString(o) + "\n");
                        }
                    }
                }
            }
//            for (String taskId : taskMap.keySet()) {
//
//            }


        } catch (IOException e) {
            log.error("", e);
        } finally {
            if (null != fileWriter) {
                try {
                    fileWriter.close();
                } catch (Exception e) {
                    log.error("", e);
                }
            }

        }

    }


}
